package service

import (
	"sync"

	"gitee.com/crack007/proxy/src/logserver/model"
)

var LogService = logService{}

type logService struct {
	queue *model.LinkedList[*model.LogModel]
	lock  sync.Mutex
}

func (x *logService) Init(buffer int) {
	x.queue = model.NewLinkedList[*model.LogModel](buffer)
}

func (x *logService) PushLog(logModel *model.LogModel) {
	x.lock.Lock()
	defer x.lock.Unlock()

	x.queue.Push(logModel)
}

func (x *logService) ListLog(length int) []*model.LogModel {
	x.lock.Lock()
	defer x.lock.Unlock()

	var list = make([]*model.LogModel, 0, length)
	for i := 0; i < length; i++ {
		v, ok := x.queue.Shift()
		if !ok {
			break
		}

		list = append(list, *v)
	}
	return list
}
